package com.zhehekeji.opsys.api.service.validate;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zhehekeji.opsys.api.code.LuckyDrawResultCode;
import com.zhehekeji.opsys.api.common.util.DateFormatUtil;
import com.zhehekeji.opsys.api.common.util.SuperResp;
import com.zhehekeji.opsys.api.entity.TWinningRule;
import com.zhehekeji.opsys.api.service.validate.args.WinningRuleValidateArgs;

/**
 * @ClassName WinningRuleValidate
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2015-4-13   上午11:41:20
 */
@Service
public class WinningRuleValidate extends ValidateService<ValidateRets<TWinningRule>, WinningRuleValidateArgs> {

	private final static Logger logger = LoggerFactory.getLogger(WinningRuleValidate.class);
	
	@Override
	public ValidateRets<TWinningRule> validate(
			WinningRuleValidateArgs validateArgs) throws Exception {
		ValidateRets<TWinningRule> validateRets = new ValidateRets<TWinningRule>();
		SuperResp resp = validateArgs.getResp();
		Long tmpActivityId = validateArgs.getActivityId();
		Date tmpCurrentDate = validateArgs.getCurrentDate();
		String tmpStrCurrentDate = DateFormatUtil.format(tmpCurrentDate, "yyyy-MM-dd");
		Long tmpCpCode = validateArgs.getCpCode();
		logger.info("中奖规则，活动id："+tmpActivityId+",CpCode="+tmpCpCode);
		String tmpWhereSql = "o.winningDate='" + tmpStrCurrentDate + "' and o.tactivity.id=?1 and o.cpCode=?2 and o.status=1 and o.tprize.status=1";
		List<TWinningRule> tmpListTWinningRule = entityDao.getScrollData(TWinningRule.class, tmpWhereSql, new Object[]{tmpActivityId, tmpCpCode});
		
		if (tmpListTWinningRule != null && !tmpListTWinningRule.isEmpty()){
			 String tmpSql = "select count(o.id) as drawCount from TDrawRecordDay o where date_format(o.drawTime,'%Y-%m-%d') = '" + tmpStrCurrentDate + "' and o.tactivity.id=? and o.cpCode=?";
			 List<Map<String, Object>> tmpListMapDrawCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{tmpActivityId, tmpCpCode}, -1, -1);
			 int  tmpIntDrawCount = 0;
			 if (tmpListMapDrawCount != null && !tmpListMapDrawCount.isEmpty()){
				 Map<String, Object> tmpMapDrawCount = tmpListMapDrawCount.get(0);
				 Long tmpLongDrawCount = (Long) tmpMapDrawCount.get("drawCount");
				 tmpIntDrawCount = tmpLongDrawCount.intValue();
			 }
			 TWinningRule tmpTWinningRule = null;
			 Integer tmpDrawSeq;
			 for (TWinningRule tmp : tmpListTWinningRule){
				 tmpDrawSeq = tmp.getDrawSeq();//抽奖序号，第几个人中奖
				 //if (tmpDrawSeq == (tmpIntDrawCount + 1)){
				 //之前已经添加了抽奖记录，这里判等就可以了
				 logger.info("第几个中奖："+tmpDrawSeq+",当天的抽奖记录数="+tmpIntDrawCount);
				 if (tmpDrawSeq == tmpIntDrawCount){
					 tmpTWinningRule = tmp;
					 break;
				 }
			 }
			 if (tmpTWinningRule == null){
				 logger.info("没有中奖规则，谢谢参与！");
				 resp.setResultCode(LuckyDrawResultCode.THANK_YOU_IN);
				 resp.setResultDesc("没有中奖规则，谢谢参与！");
				 validateRets.setSuccess(false);
				 return validateRets;
			 }
			 validateRets.setEntity(tmpTWinningRule);
		}
		return validateRets;
	}

}
